import Vue from 'vue'
import VueRouter from 'vue-router'
import BkLogin from '@/views/bklogin.vue'
import BkHome from '@/views/bkHome.vue'
import AddGoods from '@/components/page/addgoods.vue'
import Home from '@/views/home.vue'
import CustomMenage from '@/components/page/custommenage.vue'
import FinishOrder from '@/components/page/finishorder.vue'
import ExpressPage from '@/components/page/expresspage.vue'
import OrderDelivered from '@/components/page/orderdelivered.vue'
import Desploy from '@/components/page/desplay.vue'
import ShopCar from '@/components/page/shopcar.vue'
import Login from '@/components/page/login.vue'
import UserRegistration from '@/components/page/userRegistration.vue'
import UserLofin from '@/components/page/userLogin.vue'
import { getExpire } from '@/utils/index.js'
import Apply from '@/components/page/apply.vue'
import MyOrder from '@/components/page/myOrder.vue'
import SelectOrder from '@/components/page/selectOrder.vue'
import EditGoods from '@/components/page/editgoods.vue'
import Mypage from '@/components/page/mypage.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/home' },
  { path: '/home', name: 'home', component: Home },
  { path: '/desploy/:id', name: 'desplay', component: Desploy, props: true },
  { path: '/bklogin', name: 'bklogin', component: BkLogin },
  { path: '/shopcar', name: 'shopcart', component: ShopCar },
  { path: '/login', name: 'login', component: Login },
  { path: '/userregistration', name: 'userregistration', component: UserRegistration },
  { path: '/userlogin', name: 'userlogin', component: UserLofin },
  { path: '/apply', name: 'apply', component: Apply },
  { path: '/myorder', name: 'myorder', component: MyOrder },
  { path: '/selectorder', name: 'selectorder', component: SelectOrder },
  { path: '/mypage', name: 'mypage', component: Mypage },
  { path: '/bkhome', redirect: '/bkhome/addgoods' },
  {
    path: '/bkhome',
    name: 'bkhome',
    component: BkHome,
    children: [
      { path: 'addgoods', name: 'addgoods', component: AddGoods },
      { path: 'custom', component: CustomMenage },
      { path: 'express', component: ExpressPage },
      { path: 'finishorder', component: FinishOrder },
      { path: 'delivered', component: OrderDelivered },
      { path: 'editgoods', component: EditGoods }
    ]
  }
]
const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  console.log(to)
  const allowArr = ['desplay', 'shopcart', 'myorder']
  if (allowArr.indexOf(to.name) !== -1) {
    if (getExpire('accessToken') === null) {
      router.push({ name: 'userlogin' })
    } else {
      next()
    }
  } else if (to.path.match(/\/bkhome/)) {
    if (!localStorage.getItem('bklogin')) {
      router.push({ name: 'bklogin' })
    } else {
      next()
    }
  } else {
    next()
  }
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
